package com.cjm.algorithm.backtracking;

import java.util.*;

public class Ti90 {
    public static void main(String[] args) {
        Ti90 ti90 = new Ti90();
        List<List<Integer>> lists = ti90.subsetsWithDup(new int[]{1, 2, 2});
        System.err.println("OK");
    }

    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        res.clear();
        path.clear();
        boolean[] dup = new boolean[nums.length];
        Arrays.sort(nums);
        backtracking(nums, 0, dup);
        return res;
    }

    private void backtracking(int[] nums, int startIndex, boolean[] dup) {
        res.add(new ArrayList<>(path));
        for (int i = startIndex; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1] && dup[i - 1] == false) {
                continue;
            }
            path.addLast(nums[i]);
            dup[i] = true;
            backtracking(nums, i+1, dup);
            dup[i] = false;
            path.pollLast();
        }
    }
}
